package cn.itcast.service.impl;

import cn.itcast.dao.PermissionDao;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.pojo.Permission;
import cn.itcast.service.PermissionService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


@Service
@Slf4j
public class PerMissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;


    /**
     * 分页查询
     *
     * @return
     */
    @Override
    public PageResult queryPermissionList(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        String queryString = queryPageBean.getQueryString();
        //查询数据库得到权限管理集合
        Page<Permission> page = permissionDao.queryPermissionList(queryString);

        log.debug("查询出的用户权限管理:{}", page);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增
     *
     * @param name
     * @param keyword
     * @param description
     * @return
     */
    @Override
    public Boolean addPermission(String name, String keyword, String description) {
        Permission permission = new Permission();
        permission.setName(name);
        permission.setKeyword(keyword);
        permission.setDescription(description);

        return permissionDao.addPermission(permission);
    }

    /**
     * 回显数据
     *
     * @param pid
     * @return
     */
    @Override
    public Permission echoPermission(String pid) {
        return permissionDao.queryPermissionById(pid);
    }

    /**
     * 修改
     *
     * @param permission
     * @return
     */
    @Override
    public Boolean updatePermission(Permission permission) {
        return permissionDao.updatePermission(permission);
    }

    /**
     * 删除
     *
     * @param pid
     * @return
     */
    @Override
    public Boolean deletePermission(String pid) {
        return permissionDao.deletePermission(pid);
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<Permission> queryPermissioList() {
        return permissionDao.findAll();
    }
}
